$(document).ready(function () {
    $(".tips-bg").height($(window).height())
    $(".tips-content").css('top', $(window).height() / 2 - 100 - $(".tips-content").height() / 2);

    $("#profile-photo-upload").on("change", function () {
        var pic = $('#profile-photo-upload')[0].files[0];
        var file = new FormData();
        file.append("profile-photo-upload", pic);
        file.append("file", md5($.cookie('username') ) + pic.name.substring(pic.name.lastIndexOf('.')));

        $.ajax({
            url: "/action/user/uploadProfilePhoto.php",
            type: "post",
            data: file,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                alert(data);
            }
        });
    });
})

var user_xmlHttp = GetXmlHttpObject();

function show_user_detail() {
    var username = $.cookie('username');
    var profile_photo = $.cookie('profile-photo');


    $(".fancy-user-and-setting-area").html('<img src="' + profile_photo + '">');
    $(".fancy-user-and-setting-area > img").click(function () {
        window.location.href = '/user/user.html';
    });
}

function login(username, password) {
    password = md5(password);

    user_xmlHttp.onreadystatechange = login_handler;
    user_xmlHttp.open("GET", "/action/user/login.php?ADM=" + username + "&PW=" + password + "&sid=" + Math.random(), true);
    user_xmlHttp.send(null);
}

function login_handler() {
    if (user_xmlHttp.readyState == 4 || user_xmlHttp.readyState == "complete") {
        var ret = user_xmlHttp.responseText;
        if (ret.startsWith("Success")) {
            args = ret.split(" ");

            $.cookie('username', args[1], {
                expires: 7,
                path: '/'
            });

            $.cookie('profile-photo', args[2], {
                expires: 7,
                path: '/'
            });

            show_user_detail();

            $("#closeBtn").click();
        } else {
            if (ret === "Error 0x01")
                alert("用户名或密码错误");
            else if (ret == "Errpr 0x02")
                alert("请先完成邮箱地址验证");
        }
    }
}

function logon_submit() {
    if (!$("#logon-check").is(":checked")) {
        $("#logon-check").removeClass("is-valid");
        $("#logon-check").addClass("is-invalid");
        return false;
    } else {
        $("#logon-check").removeClass("is-invalid");
        $("#logon-check").addClass("is-valid");
    }

    username = $("#logon-username").val();
    email = $("#logon-email").val();
    password = $("#logon-passworrd1").val();
    password2 = $("#logon-passworrd2").val();

    if (password === password2) {
        $("#logon-passworrd2,#logon-passworrd1").removeClass("is-invalid");
        $("#logon-passworrd2,#logon-passworrd1").addClass("is-valid");
    } else {
        $("#logon-passworrd2,#logon-passworrd1").addClass("is-invalid");
        $("#logon-passworrd2,#logon-passworrd1").removeClass("is-valid");
    }

    logon(username, md5(password), email);

    return false;
}

function logon(username, password, email) {
    user_xmlHttp.onreadystatechange = logon_handler;
    user_xmlHttp.open("GET", "/action/user/logon.php?ADM=" + username + "&PW=" + password + "&EMAIL=" + email + "&sid=" + Math.random(), true);
    user_xmlHttp.send(null);
}

function logon_handler() {
    if (user_xmlHttp.readyState == 4 || user_xmlHttp.readyState == "complete") {
        var ret = user_xmlHttp.responseText;
        if (ret.startsWith("Error")) {
            if (ret === "Error 0x01") {
                $("#logon-username").addClass("is-invalid");
                $("#logon-username").removeClass("is-valid");

                $("#logon-email").removeClass("is-valid");
                $("#logon-email").removeClass("is-invalid");
            } else if (ret === "Error 0x02") {
                $("#logon-username").removeClass("is-invalid");
                $("#logon-username").addClass("is-valid");

                $("#logon-email").addClass("is-invalid");
                $("#logon-email").removeClass("is-valid");
            }
        } else if (ret === "Success") {
            $("#logon-username").removeClass("is-invalid");
            $("#logon-username").addClass("is-valid");

            $("#logon-email").removeClass("is-invalid");
            $("#logon-email").addClass("is-valid");

            window.location.href = '/user/tips.html?tip=已向您的邮箱发送了一封验证邮件';
        } else {
            $("#logon-username").removeClass("is-invalid");
            $("#logon-username").addClass("is-valid");

            $("#logon-email").removeClass("is-invalid");
            $("#logon-email").addClass("is-valid");

            alert("服务器故障");
        }
    }
}